from .celery import app
from home.models import Banner
from django.conf import settings
from django.core.cache import cache
from home.serializers import BannerModelSerializer
@app.task
def update_banner_cache():
    banner_query = Banner.objects.filter(is_delete=False,
                                        is_show=True).order_by('-order')[:settings.BANNER_COUNT]

    banner_list = BannerModelSerializer(banner_query,many=True).data
    for banner_dic in banner_list:
        banner_dic['image'] = settings.BASE_URL +  banner_dic['image']
    cache.set('banner_list',banner_list)

'''
#延迟发送任务
@app.task
def delay_sms(request,to,content):
    # 拿到前台手机
    mobile = request.data.get('mobile')
    if not (mobile and re.match(r'^1[3-9][0-9]{9}$', mobile)):
        return APIResponse(2, '手机号格式有误')
    # 获取验证码
    code = tx_sms.get_code()
    # 发送短信
    result = tx_sms.send_sms(mobile, code, settings.SMS_EXP // 60)

    # 服务器缓存验证码
    if not result:
        return APIResponse(1, '发送验证码失败')
    cache.set(settings.SMS_CACHE_KEY % mobile, code, settings.SMS_EXP)  # 缓存成功

    # 检验发送的验证码与缓存的验证码是否一致
    # print('>>>>>>>>%s - %s<<<<<<<<<<' % (code,cache.get('sms_%s' % mobile)))
    return APIResponse(0, '发送验证码成功')
'''

